package com.android.gufengmall.video;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.bean.VideoBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.VideoBeanDao;

import java.util.List;

import xyz.doikki.videoplayer.player.VideoView;
import xyz.doikki.videoplayer.util.PlayerUtils;

public class VideoDetailsActivity extends BaseActivity {

    private static final String TAG = "VideoDetailsActivity";

    private VideoView mVideoView;
    private StandardVideoController mController;

    private String mPosition;

    private App mMyApp;
    private DaoSession mDaoSession;
    private VideoBeanDao mVideoBeanDao;
    private VideoBean mVideoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_details);

        initView();
        initData();
    }

    private void initData() {
        mPosition = getIntent().getExtras().getString("position");

        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mVideoBeanDao = mDaoSession.getVideoBeanDao();


        List<VideoBean> videoBeanList = mVideoBeanDao.queryBuilder().where(VideoBeanDao.Properties.VideoTitle.eq(mPosition)).list();
        for (int i = 0; i < videoBeanList.size(); i++) {
            VideoBean videoBean = videoBeanList.get(i);
            if (videoBean.getVideoTitle().equals(mPosition)) {
                mVideoBean = videoBean;
            }
        }

        mVideoView.startFullScreen();
        mVideoView.setUrl(mVideoBean.getVideoUrl());
        mController = new StandardVideoController(this);
        mController.addControlComponent(new CompleteView(this));
        mController.addControlComponent(new ErrorView(this));
        mController.addControlComponent(new PrepareView(this));

        TitleView titleView = new TitleView(this);
        titleView.findViewById(R.id.back).setOnClickListener(v -> finish());
        titleView.setTitle(mVideoBean.getVideoTitle());
        mController.addControlComponent(titleView);
        VodControlView vodControlView = new VodControlView(this);
        vodControlView.findViewById(R.id.fullscreen).setVisibility(View.GONE);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vodControlView.findViewById(R.id.total_time).getLayoutParams();
        lp.rightMargin = PlayerUtils.dp2px(this, 16);
        mController.addControlComponent(vodControlView);
        mController.addControlComponent(new GestureView(this));
        mVideoView.setVideoController(mController);
        mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_16_9);
        mVideoView.start();
    }

    private void initView() {
        mVideoView = findViewById(R.id.video_view);
    }

    @Override
    public void onBackPressed() {
        if (!mController.isLocked()) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVideoView.stopFullScreen();
        mVideoView.pause();
        mVideoView.release();
    }
}